package org.example.rocket;

import cn.hutool.core.util.RuntimeUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class Consumer {

    public static void init() throws Exception{
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("groupB");
        consumer.setNamesrvAddr("localhost:9876");
        consumer.registerMessageListener(new MsgHandler1());
        consumer.subscribe("ShuJu","*");
        System.out.println("开始监听数据");
        consumer.start();
    }

   static class MsgHandler1 implements MessageListenerOrderly{

        @Override
        public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
            list.forEach(l->{
                String string = new String(l.getBody());

                System.out.println("消费消息");

                //故障诊断开始
                String pyPrint = RuntimeUtil.execForStr("python /home/Jinx/Desktop/print.py " + string);
                System.out.println("获取到Python打印结果："+pyPrint);
                //故障诊断结束

                //返回故障诊断结果
                try {
                    Producer.send(pyPrint,"JieGuo");
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
            return ConsumeOrderlyStatus.SUCCESS;
        }
    }
}
